package com.tomek.ktoryoperator.tasks;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;

import com.tomek.ktoryoperator.R;
import com.tomek.ktoryoperator.activities.MainActivity;
import com.tomek.ktoryoperator.adapters.MainListAdapter;
import com.tomek.ktoryoperator.db.NetworkProviderContentProvider;
import com.tomek.ktoryoperator.model.SingleEntry;
import com.tomek.ktoryoperator.providers.NumberInfoProvider;
import com.tomek.ktoryoperator.utils.ContentProvidersUtils;
import com.tomek.ktoryoperator.utils.DialogUtils;
import com.tomek.ktoryoperator.utils.LogUtils;
import com.tomek.ktoryoperator.utils.PreferenceUtils;
import com.tomek.ktoryoperator.utils.Utils;

public class FillListAdapterTask extends AsyncTask<Void, Integer, List<SingleEntry>> {
	final ContentResolver contentResolver;
	final MainActivity activity;
	ProgressDialog progressDialog;

	final Map<String, NumberInfoProvider> numberInfoProviders;

	public FillListAdapterTask(ContentResolver contentResolver, MainActivity activity,
			Map<String, NumberInfoProvider> numberInfoProviders) {
		this.contentResolver = contentResolver;
		this.activity = activity;
		this.numberInfoProviders = numberInfoProviders;
	}

	@Override
	protected void onPreExecute() {
		progressDialog = new ProgressDialog(activity);
		progressDialog.setMessage(activity.getString(R.string.loading));
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	}

	@Override
	protected List<SingleEntry> doInBackground(Void... params) {
		long start = System.currentTimeMillis();
		LogUtils.d(getClass(), "doInBackground", "begin");

		HashMap<String, SingleEntry> map = ContentProvidersUtils.readContacts(activity, contentResolver);

		fillContactsWithProviderData(map);

		ArrayList<SingleEntry> list = new ArrayList<SingleEntry>(map.values());
		Collections.sort(list, new EntryComparator(PreferenceUtils.getSorting(activity)));

		LogUtils.d(getClass(), "doInBackground", "end. time=%d ms", System.currentTimeMillis() - start);

		return list;
	}

	private void fillContactsWithProviderData(HashMap<String, SingleEntry> map) {
		if (map.size() > 0) {
			Cursor cursor2 = contentResolver.query(NetworkProviderContentProvider.CONTENT_ALL_ENTRIES_URI, null, null,
					null, null);

			while (cursor2.moveToNext()) {
				String telNr = cursor2.getString(NetworkProviderContentProvider.COLUMN_NUMBER);

				SingleEntry se = map.get(telNr);

				if (se != null) {
					String provider = cursor2.getString(NetworkProviderContentProvider.COLUMN_PROVIDER);
					String value = cursor2.getString(NetworkProviderContentProvider.COLUMN_VALUE);
					long date = cursor2.getLong(NetworkProviderContentProvider.COLUMN_DATE);
					long id = cursor2.getInt(NetworkProviderContentProvider.COLUMN_ID);

					se.date = date;
					se.id = id;
					se.valueId = value;
					se.providerId = provider;
				}
			}

			cursor2.close();
		}
	}

	@Override
	protected void onPostExecute(List<SingleEntry> param) {
		MainListAdapter ad = new MainListAdapter(activity, param, activity, this.numberInfoProviders);

		activity.setMainListAdapter(ad);
		activity.setProgressBarIndeterminateVisibility(false);

		progressDialog.dismiss();

		// DialogUtils.showTechInfoDialog(activity, false);
		DialogUtils.showTipsDialog(activity);
	}

	private static class EntryComparator implements Comparator<SingleEntry> {
		final int sorting;

		public EntryComparator(int sorting) {
			this.sorting = sorting;
		}

		public final int compare(SingleEntry a, SingleEntry b) {
			switch (sorting) {
			case PreferenceUtils.SORTING_LAST_NAME:
				return Utils.comparePL(a.lastName, b.lastName);
			case PreferenceUtils.SORTING_PHONE_NUMBER:
				return a.number.compareTo(b.number);
			default:
				return Utils.comparePL(a.abonent, b.abonent);
			}
		}
	}
}